//
//  MYTimeField.m
//  国旗注册界面
//
//  Created by   李小云 on 16/4/18.
//  Copyright © 2016年   李小云. All rights reserved.
//

#import "MYTimeField.h"
#import "SIXBasicField.h"

@interface MYTimeField ()

@property(nonatomic,strong)UIDatePicker *datePicker;
@end

@implementation MYTimeField

#pragma mark - 实现不管用户是通过init还是Xib加载键盘都会获取到自定义的键盘
- (void)awakeFromNib{
    
    [super awakeFromNib];
    [self  getTimeField];
    
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self  getTimeField];
    }
    
    return self;
}

- (void)getTimeField {
    
    self.text = @"1992-02-18";
     //从XIB获取自定义的键盘View
    SIXBasicField *view = [[[NSBundle mainBundle]loadNibNamed:@"TimeField" owner:nil options:nil]lastObject];
    
    for (UIDatePicker *datePicker in view.subviews) {
        if ([datePicker isKindOfClass:[UIDatePicker class]]) {

            _datePicker = datePicker;
        }
    }
    //当点击取消和确认时 取消键盘的第一响应
    __weak typeof(self) weakSelf = self;
    view.myblock = ^(UIButton *btn){
        
        [weakSelf resignFirstResponder];
        
    };

    //设置时间模式
    _datePicker.datePickerMode = UIDatePickerModeDate;
    //设置时间地区
    NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"ZH"];
    _datePicker.locale = locale;
    //设置时间格式
    NSDateFormatter *Formatter = [[NSDateFormatter alloc]init];
    Formatter.dateFormat = @"yyyy-MM-dd";
    //设置中间时间利与用户体验
    NSDate *date = [Formatter dateFromString:@"1992-02-18"];
    
    _datePicker.date = date;

    self.inputView  = view;
    [_datePicker addTarget:self action:@selector(getNumber:) forControlEvents:UIControlEventValueChanged];
    
}
-(void)getNumber:(UIDatePicker *)datePicker {
    
   
    NSDate *dateNumber = datePicker.date;
    NSDateFormatter *Formatter = [[NSDateFormatter alloc]init];
    Formatter.dateFormat = @"yyyy-MM-dd";
    NSString *str = [Formatter stringFromDate:dateNumber];
    self.text = str;
    
}

@end
